package impleDao;

import modelo.Alquiler;
import modelo.AlquilerPK;


import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import dao.AlquilerDAO;

public class AlquilerImple extends HibernateDaoSupport implements AlquilerDAO
{
	private static Session session;
	private static Transaction tx;
	
	private void iniciaOperacion() throws HibernateException {
	 	session = getSession();
	 	tx = session.beginTransaction();
	 }

	 private void manejaExcepcion(HibernateException he) throws HibernateException {
		 tx.rollback();
		 throw new HibernateException("ERROR en la capa de acceso a datos", he);
	 }

	@Override
	public int insert(Alquiler a)
	{
		// TODO Auto-generated method stub
		int id=0;
		try{
			iniciaOperacion();
			AlquilerPK pk = (AlquilerPK)session.save(a);
			//System.out.println(pk);
			tx.commit();
		}catch(HibernateException he){
			manejaExcepcion(he);
			throw he;
		}finally{
			session.close();
		}
		return id;
	}

}
